//
//  ITVFLViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/4.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITVFLViewController.h"

@interface ITVFLViewController ()
@property (nonatomic, strong) UILabel *desTopLabel;
@end

@implementation ITVFLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupViews2];
}
-(void)setupViews1 {
    UIView *blueView = [[UIView alloc]init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    
    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSNumber *width = @100.0;
    NSNumber *height = @100.0;
    NSDictionary *viewsDic = @{@"blueView":blueView,@"redView":redView};
    NSDictionary *metricsDic = @{@"width":width,@"height":height};
    
//    NSLayoutFormatOptions ops =   NSLayoutFormatAlignAllTop|NSLayoutFormatAlignAllBottom;
    NSLayoutFormatOptions ops =   NSLayoutFormatAlignAllRight|NSLayoutFormatAlignAllLeft;
    
    NSString *vfl1 = @"V:[blueView(width)]-30-[redView(width)]-0-|";
    
    NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:vfl1 options:ops metrics:metricsDic views:viewsDic];
    [self.view addConstraints:constraints1];
    
    NSString *vfl2 = @"H:|-0-[blueView]-0-|";
    NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:ops metrics:metricsDic views:viewsDic];
    [self.view addConstraints:constraints2];
}
-(void)setupViews2 {
    UIView *labelBox = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
    {
        [self.view addSubview:labelBox];
        self.desTopLabel = [[UILabel alloc] init];
        {
            self.desTopLabel.text = @"desTopLabel";
            self.desTopLabel.translatesAutoresizingMaskIntoConstraints = NO;
//            self.desTopLabel.font = [UIFont fontWithName:@"PingFangTC-Medium" size:24];
            self.desTopLabel.textColor = [UIColor blackColor];
            self.desTopLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
            self.desTopLabel.backgroundColor = [UIColor redColor];
            [labelBox addSubview:self.desTopLabel];
        }
        
        UILabel *desBottomLabel = [[UILabel alloc] init];
        {
            desBottomLabel.text = @"desBottomLabeldesBottomLabeldesBottomLabel";
            desBottomLabel.numberOfLines = 0;
            desBottomLabel.backgroundColor = [UIColor blueColor];
            desBottomLabel.translatesAutoresizingMaskIntoConstraints = NO;
//            desBottomLabel.font = [UIFont systemFontOfSize:14];
            desBottomLabel.textColor = [UIColor blackColor];
            desBottomLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
            [labelBox addSubview:desBottomLabel];
        }
        
        NSString *v  = @"V:|-0-[desTopLabel]-[desBottomLabel]-0-|";
        NSString *h1 = @"H:|-0-[desTopLabel]-0-|";
        NSString *h2 = @"H:|-0-[desBottomLabel]-0-|";

        //此处需要对topMargin参数进行解释, 其实就是给topMargin赋值;
        NSDictionary *viewsDic = @{@"desTopLabel":self.desTopLabel,@"desBottomLabel":desBottomLabel};
        NSDictionary *viewsDic1 = @{@"desTopLabel":self.desTopLabel};
        NSDictionary *viewsDic2 = @{@"desBottomLabel":desBottomLabel};
        NSLayoutFormatOptions ops =   NSLayoutFormatAlignAllRight|NSLayoutFormatAlignAllLeft;
        NSArray *s1T = [NSLayoutConstraint constraintsWithVisualFormat:v options:ops metrics:nil views:viewsDic];

        NSArray *s1L = [NSLayoutConstraint constraintsWithVisualFormat:h1 options:0 metrics:nil views:viewsDic1];
        NSArray *v2Tc = [NSLayoutConstraint constraintsWithVisualFormat:h2 options:0 metrics:nil views:viewsDic2];

           //一般需要把约束添加到父view上
        [labelBox addConstraints:v2Tc];
        [labelBox addConstraints:s1T];
        [labelBox addConstraints:s1L];
        
//        NSString *h1 = @"V:|-0-[desTopLabel]-0-|";
//        NSString *h2 = @"H:|-0-[desTopLabel]-0-|";
//        NSDictionary *viewsDic = @{@"desTopLabel":desTopLabel};
//        NSArray *s1L = [NSLayoutConstraint constraintsWithVisualFormat:h1 options:0 metrics:nil views:viewsDic];
//        NSArray *v2Tc = [NSLayoutConstraint constraintsWithVisualFormat:h2 options:0 metrics:nil views:viewsDic];
//        [labelBox addConstraints:s1L];
//        [labelBox addConstraints:v2Tc];
    }
}

@end
